/**
 * 竞争分析 - 编辑页面
 * @author miaoyl
 * @date 2018/01/14
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isDetail: false,
            AMOUNT: 100,
            form: {
                GWGGORIG_NAME: null,
                GWGGORIU_NAME: null,
                GWGC022: null
            },
            rules: {
                GWGGORIU_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGGORIG_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGG001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGG002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGG003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGG004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                // GWGG005: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                // GWGG006: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                GWGG007: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGG008: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGG009: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                // GWGG010: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                // GWGG011: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                // GWGG012: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                GWGG013: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGG014: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGG015: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                // GWGG016: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ]
            },
            selectOptions: {
                sys_n_y: app.datas_('sys_n_y'),
                gwgg004Options: app.datas_('crm_gwgi004_gwgg004'),
                gwgg006Options: app.datas_('crm_gwgi004_gwgg006')
            },
        },

        cols_gwgh = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWGH002', width: 60, title: '项次'}
            , {field: 'GWGH003', width: 100, title: '标段号'}
            /*, {field: 'GWGH004', width: 120, title: '车辆型号'}*/
            , {field: 'GWGH005', width: 120, title: '产品编号'}
            /*, {field: 'GWAML004', width: 80, title: '规格'}*/
            , {field: 'GWAHL003', width: 80, title: '产品大类'}
            , {field: 'GWAOL003', width: 80, title: '车辆类型'}
            , {field: 'GWGH006', width: 140, title: '竞争对手产品优势', edit: 'text'}
            , {field: 'GWGH007', width: 140, title: '竞争对手产品劣势', edit: 'text'}
            , {field: 'GWGH008', width: 140, title: '竞争对手产品报价', edit: 'text'}
            , {field: 'GWGH009', width: 100, title: '我司产品优势', edit: 'text'}
            , {field: 'GWGH010', width: 100, title: '我司产品劣势', edit: 'text'}
            , {field: 'GWGH011', width: 100, title: '我司产品报价', edit: 'text'}
        ]],
        pageVue,
        table,
        callObj, layOpenIndex,
        gwghTable
    ;

    var Gwgi004Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwgi004_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: '/common/_open_window'
                }]
            }, win);
        },

        initGwghTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwghTable = table.render({
                    elem: app.getByid(doc, 'gwgi004_gwgh_table'),
                    id: 'gwgi004_gwgh_table',
                    cols: cols_gwgh,
                    height: '300',
                    page: false
                });

                //监听单元格编辑
                table.on('edit(gwgi004_gwgh_table)', function (obj) {
                    var oldValue = $(this).prev().text();
                    if(obj.field == 'GWGH008'){
                        var re = /^\d+(?=\.{0,1}\d+$|$)/; //判断是否正数
                        if (!re.test(obj.value)) {
                            obj.updateOfEdit({
                                GWGH008: oldValue
                            });
                            app.showWarning('产品报价必须为正数！');
                            return;
                        }
                    }else if(obj.field == 'GWGH011'){
                        var reg = /^\d+(?=\.{0,1}\d+$|$)/;
                        if(!reg.test(obj.value)){
                            obj.updateOfEdit({
                                GWGH011: oldValue
                            });
                            app.showWarning('产品报价必须为正数！');
                            return;
                        }
                    }
                });

                table.on('tool(gwgi004_gwgh_table)', function (obj) {

                });
            });
        },

        editData: function (listData) {
            app.ajax({
                xurl: 'crm/gwgi004/editData',
                xdata: {gwghList:listData,gwggData:vueData.form},
                loading: true,
                toJsonStr: true,
                okmsg: '竞争分析修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwgi004').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },

        addData: function (listData) {
            app.ajax({
                xurl: 'crm/gwgi004/addData',
                xdata: {gwghList:listData,gwggData:vueData.form},
                loading: true,
                toJsonStr: true,
                okmsg: '竞争分析保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwgi004').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },

        calculate:function (num, digit) {
            var data = math.div(Math.round(math.mul(num, digit)),digit);
            return data;
        },

        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            layOpenIndex = obj.layOpenIndex;
            if(obj.is_detail){
                vueData.isDetail = true;
            }else {
                vueData.isDetail = false;
            }
            if (obj.is_add) {
                vueData.isAdd = true;
                app.ajax({
                    xurl: 'crm/gwgi004/getAddData',
                    xdata: {},
                    cb: function (result) {
                        if (result.ok) {
                            console.log('result.data.data',result.data.data);
                            app.extendUpLow({
                                target: vueData.form,
                                res: [result.data.data,
                                    {
                                        GWGGACTI: 'Y'
                                    }
                                ],
                                reset: true
                            });
                        }
                    }
                });
                self.gwghTableReload([]);
            } else {
                vueData.isAdd = false;
                callObj = obj.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                app.ajax({
                    xurl: 'crm/gwgi004/getTable',
                    xdata: {
                        GWGGID: callObj.data.GWGGID,
                        GWGGSEQ: callObj.data.GWGGSEQ},
                    cb: function (result) {
                        if (result.ok) {
                            var gwghList = result.data.data;
                            self.gwghTableReload(gwghList);
                        }
                    }
                });
            }
            pageVue.$nextTick(function(){  //使用vue.$nextTick()方法可以dom数据更新后延迟执行
                pageVue.$refs['form'].clearValidate();
            });
        },

        gwghTableReload: function (listData) {
            self._reloadTable({id: 'gwgi004_gwgh_table', data: listData});
        },

        _ready: function () {
            vueData.form = app.fields_(['GWGG_T'], vueData.form);
            pageVue = new Vue({
                el: '#gwgi004_edit',
                data: vueData,
                methods: {
                    openGwgc001: function () {//招标编号开窗
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwgc001',
                                    columnList: [
                                        {label: '投标ID', prop: 'GWGCID', width: '150'},
                                        {label: '招标编号', prop: 'GWGC001', width: '150'},
                                        {label: '项目名称', prop: 'GWGC022', width: '150'},
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWGGID = data.GWGCID;
                                    vueData.form.GWGG001 = data.GWGC001;
                                    vueData.form.GWGC022 = data.GWGC022;
                                    self.gwghTableReload([]);
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择投标信息',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                var flag = true;
                                var listData = gwghTable.getDatas();
                                listData.forEach(function (v) {
                                    if(v.GWGH006 == '' || v.GWGH006 == null ){
                                        app.showWarning("竞争对手产品优势必填！");
                                        flag = false;
                                        return;
                                    }
                                    if(v.GWGH007 == '' || v.GWGH007 == null ){
                                        app.showWarning("竞争对手产品劣势必填！");
                                        flag = false;
                                        return;
                                    }
                                    if(v.GWGH008 == '' || v.GWGH008 == null ){
                                        app.showWarning("竞争对手产品报价必填！");
                                        flag = false;
                                        return;
                                    }
                                    if(v.GWGH009 == '' || v.GWGH009 == null ){
                                        app.showWarning("我司产品优势必填！");
                                        flag = false;
                                        return;
                                    }
                                    if(v.GWGH010 == '' || v.GWGH010 == null ){
                                        app.showWarning("我司产品劣势必填！");
                                        flag = false;
                                        return;
                                    }
                                    if(v.GWGH011 == '' || v.GWGH011 == null ){
                                        app.showWarning("我司产品报价必填！");
                                        flag = false;
                                        return;
                                    }
                                });
                                if(flag) {
                                    if (vueData.isAdd) {
                                        self.addData(listData);
                                    } else {
                                        self.editData(listData);
                                    }
                                }
                            }
                        });
                    },
                    addGwgh:function () {
                        //根据投标编号获取投标信息
                        app.ajax({
                            xurl: 'crm/gwgi004/getGwgdData',
                            xdata: {GWGDID: vueData.form.GWGGID},
                            cb: function (result) {
                                if (result.ok) {
                                    var gwgdData = result.data.data;
                                    var gwgdDataNew = app.cloneJSON(gwgdData);
                                    if(gwgdData.length > 0){
                                        var choose_data = gwghTable.getDatas();
                                        var gwghData = app.cloneJSON(choose_data);
                                        var count = 0;
                                        if(choose_data.length > 0) {
                                            count = choose_data[choose_data.length - 1].GWGH002;
                                        }
                                        if(gwghData.length > 0){
                                            gwgdDataNew.forEach(function (item) {
                                                gwghData.forEach(function (v) {
                                                    if(item.GWGD003 == v.GWGH003 /*&& item.GWGD005 == v.GWGH004*/ && item.GWGD006 == v.GWGH005){
                                                        gwgdData.removeByMap({
                                                            GWGD002: item.GWGD002
                                                        });
                                                    }
                                                });
                                            });
                                        }
                                        gwgdData.forEach(function (item) {
                                            choose_data.push({
                                                GWGH002: count+1,
                                                GWGH003: item.GWGD003,
                                                /*GWGH004: item.GWGD005,*/
                                                GWGH005: item.GWGD006,
                                                /*GWAML004: item.GWAML004,*/
                                                GWAHL003: item.GWAHL003,
                                                GWAOL003: item.GWAOL003,
                                                GWGH006: '',
                                                GWGH007: '',
                                                GWGH008: '',
                                                GWGH009: '',
                                                GWGH010: '',
                                                GWGH011: ''
                                            });
                                            count++;
                                        });
                                        self.gwghTableReload(choose_data);
                                    }
                                }
                            }
                        });
                    },
                    delGwgh:function () {
                        var listData = gwghTable.getDatas();
                        var checkStatus = table.checkStatus('gwgi004_gwgh_table');
                        if (checkStatus.data.length > 0) {
                            checkStatus.data.forEach(function (v) {
                                listData.removeByMap({
                                    GWGH002: v.GWGH002
                                });
                            });
                            if (checkStatus.length != 0) {
                                var i = 1;
                                listData.forEach(function (v) {
                                    v.GWGH002 = i;
                                    i++;
                                });
                            }
                            self.gwghTableReload(listData);
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwgi004_edit');
                },
                watch: {
                }
            });

            self.initGwghTable();
        },

        _lazy: function () {
            this._super();

            app.ajax({
                xurl: 'admin/getDigit',
                cb: function (result) {
                    if(result.ok) {
                        var data = result.data.data;
                        vueData.AMOUNT = Math.pow(10,data.amount);
                    }else {
                        console.log("获取小数位失败");
                    }
                }
            });
        }
    });

    return new Gwgi004Edit();

});